package com.example.shopping_mall.fragment;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;

import com.example.shopping_mall.R;
import com.example.shopping_mall.adapter.RoderAdapter;
import com.example.shopping_mall.application.MineOrderPresenter;
import com.example.shopping_mall.bean.EndMineOrder;
import com.example.shopping_mall.bean.MineOrder;
import com.example.shopping_mall.bean.OrderItemList;
import com.example.shopping_mall.http.IMineOrderPresenter;

import java.util.ArrayList;
import java.util.List;

/**
 * 待评价evaluate
 */
public class MineEvaluateFragment extends AllFragment {

    RecyclerView evaluateRV;
    public IMineOrderPresenter mineOrderPresenter;

    OrderItemList itemList;
    EndMineOrder endMineOrder;
    MineOrder order;

    @Override
    protected void initView() {
        mineOrderPresenter = new MineOrderPresenter(getContext());
        //初始化列表
        evaluateRV = findViewById(R.id.evaluateRV);
        evaluateRV.setLayoutManager(new LinearLayoutManager(getActivity()));
        //initEvaluate();
        initData();
    }

    private void initData() {

        mineOrderPresenter.requestOrder(1, new MineOrderPresenter.OnMineOrderListener() {
            @Override
            public void onMineOrder(MineOrder mineOrder) {
                //evaluateRV.setVisibility(View.VISIBLE);
                if (mineOrder.getStatus() == 3) {
                    List<Object> listO = new ArrayList<>();
                    List<List<OrderItemList>> orderList = new ArrayList<>();
                    //System.out.println("订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单': " + mineOrder.getStatus());
                    OrderItemList[] orderItemLists = mineOrder.getOrderItemList();
                    List<OrderItemList> listitem = new ArrayList<>();
                    for (int i = 0; i < orderItemLists.length; i++) {
                        itemList = new OrderItemList();
                        itemList.setProductPrice(orderItemLists[i].getProductPrice());
                        itemList.setProductName(orderItemLists[i].getProductName());
                        itemList.setProductPic(orderItemLists[i].getProductPic());
                        listitem.add(itemList);
                    }
                    orderList.add(listitem);


                    //拆分数据
                    if (orderList != null) {
                        for (int i = 0; i < orderList.size(); i++) {
                            order = new MineOrder();
                            order.setOrderSn(mineOrder.getOrderSn());
                            order.setTotalAmount(mineOrder.getTotalAmount());
                            order.setPayAmount(mineOrder.getPayAmount());
                            order.setStatus(mineOrder.getStatus());

                            endMineOrder = new EndMineOrder();
                            endMineOrder.setOrderSn(mineOrder.getOrderSn());
                            endMineOrder.setTotalAmount(mineOrder.getTotalAmount());
                            endMineOrder.setPayAmount(mineOrder.getPayAmount());
                            endMineOrder.setStatus(mineOrder.getStatus());


                            listO.add(order);
                            listO.addAll(orderList.get(i));
                            listO.add(endMineOrder);
                        }
                    }
                    RoderAdapter adapter = new RoderAdapter(listO, getActivity());
                    evaluateRV.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                } else {
                   findViewById(R.id.evaluateNullOrder).setVisibility(View.VISIBLE);
                }
            }
        });
    }
    /*private void initEvaluate() {
        ArrayList<MineOrder> list = new ArrayList<>();
        mineOrderPresenter.requestOrder(1, new MineOrderPresenter.OnMineOrderListener() {
            @Override
            public void onMineOrder(MineOrder mineOrder) {
                order = new MineOrder();
                if (mineOrder.getStatus() == 3) {
                    order.setOrderSn(mineOrder.getOrderSn());
                    order.setTotalAmount(mineOrder.getTotalAmount());
                    order.setPayAmount(mineOrder.getPayAmount());
                    order.setStatus(mineOrder.getStatus());
                    //System.out.println("订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单': " + mineOrder.getStatus());
                    list.add(order);
                    OrderItemList[] itemList = mineOrder.getOrderItemList();
                    for (int i = 0; i < itemList.length; i++) {
                        int postion = 0;
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                URL imageurl = null;
                                try {
                                    imageurl = new URL(itemList[2].getProductPic());
                                } catch (MalformedURLException e) {
                                    e.printStackTrace();
                                }
                                try {
                                    HttpURLConnection conn = (HttpURLConnection) imageurl.openConnection();
                                    conn.setDoInput(true);
                                    conn.connect();
                                    InputStream is = conn.getInputStream();
                                    bitmap = BitmapFactory.decodeStream(is);
                                    getActivity().runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            View view = findViewById(R.id.inc);
                                            imageSU = view.findViewById(R.id.imageSU);
                                            imageSU.setImageBitmap(bitmap);
                                            textcommodity = view.findViewById(R.id.textcommodity);
                                            textcommodity.setText(itemList[postion].getProductName());

                                            imageSU = view.findViewById(R.id.imageSU);
                                            imageSU.setImageBitmap(bitmap);
                                        }
                                    });

                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }).start();
                    }
                    //设置适配器
                    //new的时候，把它的子布局和数据传入
                    mineOrderAdapter = new MineOrderAdapter(list, getContext());
                    //给列表添加适配器
                    evaluateRV.setAdapter(mineOrderAdapter);
                }
            }
        });
    }*/

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine_evaluate;
    }
}